package me.uniauto.service.impl;

import com.github.pagehelper.PageHelper;
import me.uniauto.Constants.HttpConstants;
import me.uniauto.bean.MUser;
import me.uniauto.bean.MUserCode;
import me.uniauto.dao.persistence.MUserMapper;
import me.uniauto.service.BaseService;
import me.uniauto.service.IAdapter;
import me.uniauto.service.IUserService;
import me.uniauto.util.CommResp;
import me.uniauto.util.MyStringUtil;
import me.uniauto.util.RequestUtil;
import me.uniauto.util.cipher.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * Created by hardy on 2018/2/22.
 */
@Service
public class UserServiceImpl extends BaseService<MUser> implements IUserService{


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MUserMapper userMapper;
    @Autowired
    private CacheServiceImpl cacheService;


    @Override
    public CommResp getUserOneInfo(MUser userVO) throws Exception {

//        PageHelper.startPage(1,10,false);
//        List<MUser> pageUser = userMapper.getUserInfoPageHelper();



        /*
        *从数据库中获取
        *
        MUser selUser = new MUser();
        selUser.setUserId(Integer.parseInt(map.get("userId") + ""));

        MUser userVO = selectOne(selUser);
        if(userVO==null){
            return CommResp.no_data_resp;
        }*/
        return success(userVO);
    }

    @Override
    public CommResp getUserInfo() throws Exception {

        Example example = new Example(MUser.class);
        PageHelper.startPage(1,10,false);
        List<MUser> userList = selectByExampleSelective(example);
        if(userList.size()==0){
            return CommResp.no_data_resp;
        }

        return success(userList);
    }

//    /**
//     * 更改用户验证码信息
//     * @param request
//     * @return
//     * @throws Exception
//     *
//     * 1、更改数据库
//     * 2、更改redis中的缓存数据
//     */
//    @Override
//    public CommResp updateUserCodeInfo(HttpServletRequest request) throws Exception {
//        Map map = selectParamMap(request);
//
//        //更改数据库
//        userMapper.updateUserCodeInfo(map);
//        //更改缓存
//        cacheService.updateUserCode(Integer.parseInt(map.get("userCodeId")+""),map);
////        MemberRedis memberRedis = new MemberRedis();
////        memberRedis.setKeyRedis("user_code_info_"+map.get("userCodeId")+"");
////        MUserCode userCodeVO = new MUserCode();
////        userCodeVO.setUserId(Integer.parseInt(map.get("userId")+""));
////        userCodeVO.setUserCode(map.get("userCode")+"");
////        userCodeVO.setUserCodeId(Integer.parseInt(map.get("userCodeId")+""));
////        memberRedis.setValueRedis(userCodeVO.getUserCode()+","+userCodeVO.getUserCodeId());
////        redisDao.add(memberRedis);
//        return success();
//    }
//
//    /**
//     * 从缓存中获取用户验证码信息
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public CommResp getUserCodeInfo(HttpServletRequest request) throws Exception {
//        Map map = selectParamMap(request);
//        if(MyStringUtil.isEmpty(map.get("userCodeId")+"")){
//            return CommResp.args_error_resp;
//        }
////        MemberRedis memberRedis = redisDao.get("user_code_info_"+map.get("userCodeId")+"");
////
////        String userCodeVO =  memberRedis.getValueRedis();
////        MUserCode userCodeVO = new MUserCode();
////        userCodeVO = cacheService.selUserCodeInfo(Integer.parseInt(map.get("userCodeId")+""),userCodeVO);
////        MUserCode userCodeVO = (MUserCode) redisDao.getValue("user_code_info_"+map.get("userCodeId")+"");
//        MUserCode userCodeVO = cacheService.selUserCodeInfo(Integer.parseInt(map.get("userCodeId")+""));
//        return success(userCodeVO);
//    }
//
//    /**
//     * 登陆
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public CommResp login(HttpServletRequest request) throws Exception {
//        Map map = selectParamMap(request);
//        if(MyStringUtil.isEmpty(map.get("mobile")+"")||MyStringUtil.isEmpty(map.get("password")+"")){
//            return CommResp.args_error_resp;
//        }
//        Example example = new Example(MUser.class);
//        Example.Criteria criteria = example.createCriteria() ;
//        criteria.andEqualTo("mobile",map.get("mobile")+"");
//
//        List<MUser> mUserList = selectByExampleSelective(example);
//        MUser mUserVO = mUserList.get(0);
//        if(!MD5.GetMD5Code(map.get("password")+"").equals(mUserVO.getPassword())){
//            return CommResp.args_error_resp;
//        }
//        request.getSession().setAttribute(HttpConstants._session,mUserVO);
//
//        return success();
//    }

    @Override
    protected IAdapter createAdapter() {
        return null;
    }



}
